//
//  BBSViewController.m
//  DianNaoWang
//
//  Created by dllo on 15/11/2.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "BBSViewController.h"
#import "Header.h"
#import "ChannelButtonView.h"
#import "PageCollectionCell.h"
#import "SecondPageCollectionViewCell.h"
#import "ForumViewController.h"
#import "SearchBarViewController.h"
#import "CFWebViewController.h"
#import "FavoriteWapPage.h"
#pragma mark 第三页 总页面
@interface BBSViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,PageCollectionCellDelegate,SecondPageCollectionViewCellDelegate>
@property(nonatomic, retain)ChannelButtonView *topView;
@property(nonatomic,retain)UICollectionView *collectionView;

@end

@implementation BBSViewController

-(void)loadView
{
    [super loadView];
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    self.view.backgroundColor = BackgroundColor;
    self.navigationController.navigationBar.translucent = NO;
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"6@2x"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(searchAction)];
    
    
    
}

#pragma mark 跳到搜索界面
-(void)searchAction
{
    SearchBarViewController *SBvc = [[SearchBarViewController alloc]init];
    SBvc.keyID = 3;
    SBvc.modalTransitionStyle = 2;
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:SBvc];
    [self presentViewController:nav animated:YES completion:NULL];
    //[self presentViewController:SBvc animated:YES completion:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _topView = [[ChannelButtonView alloc]initWithFrame:CGRectMake(0, 0, 200 * FitWidth, 30 * FitHeight)];
    [_topView.leftButton addTarget:self action:@selector(leftButtonAction) forControlEvents:UIControlEventTouchUpInside];
    [_topView.rightButton addTarget:self action:@selector(rightButtonAction) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.titleView = _topView;
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake(ScreenWidth, ScreenHeight - (49 + 64));
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 0;
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - (49 + 64)) collectionViewLayout:flowLayout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.pagingEnabled = YES;
    _collectionView.showsHorizontalScrollIndicator = NO;
    _collectionView.showsVerticalScrollIndicator = NO;
    _collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_collectionView];
    
    [_collectionView registerClass:[SecondPageCollectionViewCell class] forCellWithReuseIdentifier:@"identifier"];
    [_collectionView registerClass:[PageCollectionCell class] forCellWithReuseIdentifier:@"identifier2"];
    
    //夜间模式的监听者
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(colorChange) name:@"colorChange" object:nil];
}
#pragma mark - 夜间模式 监听到变化后采取的措施
- (void )colorChange
{
    self.view.backgroundColor = BackgroundColor;
    _collectionView.backgroundColor = BackgroundColor;
    [_collectionView reloadData];
}

#pragma mark - collectionView的代理方法
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 2;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        PageCollectionCell *cell1 = [collectionView dequeueReusableCellWithReuseIdentifier:@"identifier2" forIndexPath:indexPath];
        [cell1.tableView reloadData];
        cell1.tableView.backgroundColor = BackgroundColor;
        cell1.HUD.backgroundColor = BackgroundColor;
        cell1.delegate = self;
        return cell1;
    }else{
        SecondPageCollectionViewCell *cell2 = [collectionView dequeueReusableCellWithReuseIdentifier:@"identifier" forIndexPath:indexPath];
        [cell2.tableView reloadData];
        cell2.HUD.backgroundColor = BackgroundColor;
        cell2.tableView.backgroundColor = BackgroundColor;
        cell2.delegate = self;
        return cell2;
    }
    
}

#pragma mark - PageCollectionCell的代理方法
-(void)passValueAndPushTuWebViewByHotArticleModel:(HotArticleModel *)hotArticleModel
{
    CFWebViewController *CFWVC = [[CFWebViewController alloc]init];
    NSString *urlstring = [NSString stringWithFormat:@"http://lib.wap.zol.com.cn/bbs/ios/detail.php?bbs=%@&bbsid=%ld&boardid=%@&bookid=%@&type=0&page=1&picOpen=1&fontSize=middle&fromType=&vs=430&ssid=(null)", hotArticleModel.bbs, hotArticleModel.bbsid, hotArticleModel.boardid, hotArticleModel.bookid];
    FavoriteWapPage *faWapPage = [[FavoriteWapPage alloc]init];
    faWapPage.wapUrlString = urlstring;
    faWapPage.title = hotArticleModel.title;
    CFWVC.favorWap = faWapPage;
    [self.navigationController pushViewController:CFWVC animated:YES];
}

#pragma mark - SecondPageCollectionCell的代理方法
-(void)pushAndpassValue:(RowListModel *)rowListModel
{
    ForumViewController *Fvc = [[ForumViewController alloc]init];
    Fvc.RListM = rowListModel;
    [self.navigationController pushViewController:Fvc animated:YES];
}

#pragma mark - 同步页面和频道标题
//滚动View 标题动
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (scrollView == _collectionView) {
        CGPoint point = _collectionView.contentOffset;
        NSInteger x = point.x / ScreenWidth;
        _topView.barView.frame = CGRectMake(x * 100 + 4, 28, 40, 2);
    }
}

//点击标题 View动
- (void)leftButtonAction
{
    _topView.barView.frame = CGRectMake(4, 28, 40, 2);
    _collectionView.contentOffset = CGPointMake(0, 0);
    
}

- (void)rightButtonAction
{
    _topView.barView.frame = CGRectMake(104, 28, 40, 2);
    _collectionView.contentOffset = CGPointMake(ScreenWidth, 0);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
